package com.cskaoyan.mall.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.Admin;
import com.cskaoyan.mall.utils.DruidUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackgeName: com.cskaoyan.mall.dao
 * @ClassName: AdminDaoImpl
 * @Author: admin
 * Date: 2020/4/29 23:44
 * project name: Javaproject1
 * @Version:
 * @Description:
 */
public class AdminDaoImpl implements AdminDao {
    public int login(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            Admin ad = queryRunner.query("select * from admin where email = ? and pwd = ?",
                    new BeanHandler<>(Admin.class),
                    admin.getEmail(),
                    admin.getPwd());
            if (ad != null)
                return 200;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 500;
    }

    @Override
    public List<Admin> allAdmins() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());

        List<Admin> admins = null;
        try {
            admins = runner.query("select * from admin", new BeanListHandler<Admin>(Admin.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    @Override
    public int addAdminss(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            //返回加入的admin的行位置
            int ad = queryRunner.update("update into admin where email = ? and nickname = ? and pwd = ?",
                    allAdmins(), admin.getEmail(), admin.getNickname(), admin.getPwd());
            //
            if (ad != -1) {
                return 200;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 500;
    }

    /**
     * 构建不同的SQL语句
     * @param admin
     * @return
     */
    @Override
    public List<Admin> getSearchAdmins(Admin admin) {
        Map<String, Object> dynamicSql = getDynamicSQLParams(admin);
        String Sql = (String) dynamicSql.get("sql");
        List<String> paramList = (List<String>) dynamicSql.get("params");
        System.out.println(Sql);

        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query(Sql, new BeanListHandler<>(Admin.class), paramList.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    /**
     * 拼出动态的SQL语句以及对应的参数
     * @param admin
     * @return
     */
    private Map<String, Object> getDynamicSQLParams(Admin admin) {
        String baseSql = "select * from admin where 1 = 1 ";

        //能否联合查询？

        List<String> list = new ArrayList<>();

        if (!StringUtils.isEmpty(admin.getEmail())) {
            baseSql = baseSql + " and email like ?";
            //模糊查询 ?和%一起联用
            list.add("%" + admin.getEmail() + "%");
        }
        if (!StringUtils.isEmpty(admin.getNickname())) {
            baseSql = baseSql + " and nickname like ?";
            list.add("%" + admin.getNickname() + "%");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sql", baseSql);
        map.put("params", list);
        return map;
    }

    @Override
    public int updateAdminss(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

        try {
            int ad = queryRunner.update("update into admin where email = ? and nickname = ? and pwd = ?",
                    allAdmins(), admin.getEmail(), admin.getNickname(), admin.getPwd());
            if (ad != -1) {
                return 200;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //
        return 500;
    }
}
